<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>学院管理</el-breadcrumb-item>
      <el-breadcrumb-item>院部列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card>
      <!-- 搜索与添加区域-->
      <el-row :gutter= "5">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select" clearable @clear="getCollegeListLimit" >
            <el-button slot="append" icon="el-icon-search" ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加院部</el-button>
        </el-col>
      </el-row>

      <!-- 学院列表主体区域 -->
      <el-table :data="collegeList" border stripe>
        <el-table-column align="center" type="index" label="#"></el-table-column>
        <el-table-column align="center" prop="collegeName" label="院部名称"></el-table-column>
        <el-table-column align="center" label="操作" width="200px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditDialog(scope.row.collegeName)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteCollege(scope.row.collegeName)" ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

      <!-- 添加院部对话框 -->
      <el-dialog
        title="添加院部"
        :visible.sync="addDialogVisible"
        width="50%"
        @close="addDialogClosed">
        <!-- 主体区 -->
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
            <el-form-item label="院部名称" prop="collegeName">
              <el-input v-model="addForm.collegeName"/>
            </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false" >取 消</el-button>
            <el-button type="primary" @click="addCollege" >确 定</el-button>
          </span>
      </el-dialog>

      <!-- 编辑院部对话框 -->
      <el-dialog
        title="编辑院部信息"
        :visible.sync="editDialogVisible"
        widt="50%"
        @close="addDialogClosed">
        <!-- 主体区 -->
        <el-form :model="editForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
          <el-form-item label="院部名称" prop="collegeName">
            <el-input v-model="editForm.collegeName"/>
          </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false" >取 消</el-button>
            <el-button type="primary" @click="editCollege" >确 定</el-button>
          </span>
      </el-dialog>

    </el-card>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        /* 获取学院列表的参数对象 */
        queryInfo: {
          query: '',
          //当前的页数
          pageNum: 1,
          //当前每页显示多少条数据
          pageSize: 5
        },
        /* total总数*/
        total: 0,
        /* 学院列表集合 */
        collegeList: [],
        /* 控制添加院部的对话框的显示/隐藏 */
        addDialogVisible: false,
        /* 控制修改院部的对话框的显示/隐藏 */
        editDialogVisible: false,
        /* 添加院部的表单数据 */
        addForm: {
          collegeName: ''
        },
        /* 编辑院部的表单数据 */
        editForm: {
          collegeId: '',
          collegeName: ''
        },
        /* 表单验证规则 */
        addFormRules: {
          collegeName: [
            {required: true, message: '请输入院部名称', trigger: 'blur'},
            {min: 2, max: 12, message: '长度为2-12个字符之间', trigger: 'blur'}
          ]
        }

      }
    },

    created() {
      this.getCollegeListLimit();
    },

    methods: {
      /* 获取院部列表集合 */
      async getCollegeListLimit() {
        const { data: res } = await this.$axios.get(this.$http+'/collegeManageController/getCollegeListLimit', {
          params: this.queryInfo
        });
        this.collegeList = res.datas.collegeList;
        this.total = res.datas.total;
      },

      /* 添加院部 */
      addCollege() {
        //进行表单验证
        this.$refs.addFormRef.validate(valid => {
          //如果valid为false，则直接退出
          if (!valid) return;
          //发送网络请求，判断准备添加的院部是否已经存在了
          this.$axios.get(this.$http+'/collegeManageController/checkCollegeName?collegeName='+this.addForm.collegeName).then(res => {
            if (res.data.code !== 200)
              return this.$message.warning('该院部已录入');
            //发送添加请求
            this.$axios.post(this.$http+'/collegeManageController/addCollege',this.addForm).then(res => {
              if (res.data.code === 200)
                this.$message.success("添加院部成功！");
              else if (res.data.code === 100 )
                this.$message.error("添加院部失败！");
              //隐藏添加对话框
              this.addDialogVisible = false;
              //刷新院部列表
              this.getCollegeListLimit();
            }).catch(err => err)
          }).catch(err => {
            console.log('异常错误',err)
            this.$message.error("后台异常，请联系维护人员")
          })
        })
      },

      /* 显示编辑对话框 */
      showEditDialog(collegeName) {
        //发送网络请求，获取被编辑院部的信息
        this.$axios.get(this.$http+'/collegeManageController/getCollegeInfo?collegeName='+collegeName).then(res => {
          if (res.data.code !== 200)
            return this.$message.warning("院部信息获取失败");
          this.editForm = res.data.datas.collegeInfo;
        }).catch(err => {
          this.$message.error("后台异常，请联系维护人员");
        });
        //显示对话框
        this.editDialogVisible = true;
      },

      /* 提交编辑信息 */
      editCollege() {
        this.$refs.addFormRef.validate(valid =>  {
          if (!valid ) return;
          //发送put请求
          this.$axios.put(this.$http+'/collegeManageController/editCollege', this.editForm).then(res => {
            if (res.data.code === 100)
              this.$message.warning("修改院部信息失败。");
            else if (res.data.code === 200)
              this.$message.success("修改院部信息成功。");
            //隐藏对话框
            this.editDialogVisible = false;
            //刷新列表
            this.getCollegeListLimit();
          }).catch(err => {
            this.$message.error("后台异常，请联系维护人员")
          })
        })
      },

      /* 删除院部 */
      async deleteCollege(collegeName) {
        //弹框是否确认删除
        const confirmResult = await this.$confirm('此操作将删除该院部所有信息, 是否继续?', '提示', {
          confirmButtonText: '删除',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err);
        if (confirmResult !== 'confirm'){ //如果返回字符串不是 confirm，则返回字符串为cancel
          return this.$message.info('已取消删除')
        }else { //发送网络请求
          this.$axios.delete(this.$http+'/collegeManageController/deleteCollege?collegeName='+collegeName).then(res => {
            if (res.data.code === 200)
              this.$message.success("删除院部信息成功！");
            else if (res.data.code === 100)
              this.$message.error("删除院部信息失败！");
            //刷新宿管列表
            this.getCollegeListLimit();
          }).catch(err => {
             this.$message.error("异常错误！" + err)
          })
        }


      },

      /* 监听 pageSize 改变的事件 */
      handleSizeChange(newSize) {
        this.queryInfo.pageSize = newSize;
        //每次改变之后，重新获取数据
        this.getCollegeListLimit();
      },

      /* 监听 pageNum 改变的事件 */
      handleCurrentChange(newPage) {
        this.queryInfo.pageNum = newPage ;
        //每次改变之后，重新获取数据
        this.getCollegeListLimit();
      },

      /* 监听添加对话框的关闭， 对其内容进行清空 */
      addDialogClosed() {
        this.$refs.addFormRef.resetFields();
      },
    }


  }
</script>

<style scoped>

</style>
